package com.wanghlme.day27.util;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

//封装固定的部分,变化的部分交给接口去完成
public class JdbcTemplate<T> {
    // 查询多条记录
    public List<T> queryForList(String sql, Object[] args, IRowWrapper<T> wrapper) {
        List<T> list = new ArrayList<>();
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            conn = DBUtil.getConn();
            pstmt = conn.prepareStatement(sql);
            if (args != null) {// 参数赋值
                for (int i = 0; i < args.length; i++) {
                    pstmt.setObject(i + 1, args[i]);
                }
            }
            rs = pstmt.executeQuery();// 执行查询
            while (rs.next()) {
                T t = wrapper.transfer(rs);
                list.add(t);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeAll(rs, pstmt, conn);
        }
        return list;
    }

    // 查询单条记录
    public T queryForOne(String sql, Object[] args, IRowWrapper<T> wrapper) {
        T t = null;
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            conn = DBUtil.getConn();
            pstmt = conn.prepareStatement(sql);
            if (args != null) {// 参数赋值
                for (int i = 0; i < args.length; i++) {
                    pstmt.setObject(i + 1, args[i]);
                }
            }
            rs = pstmt.executeQuery();// 执行查询
            if (rs.next()) {
                t = wrapper.transfer(rs);//调用回调接口中的方法完成数据的组装
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeAll(rs, pstmt, conn);
        }
        return t;
    }

    // 增，删，改
    public int executeUpdate(String sql, Object[] args) {
        int rowCount = 0;
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            // Class.forName("com.mysql.jdbc.Driver");
            // conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/warehouse",
            // "root", "123");
            conn = DBUtil.getConn();
            pstmt = conn.prepareStatement(sql);
            if (args != null) {// 参数赋值
                for (int i = 0; i < args.length; i++) {
                    pstmt.setObject(i + 1, args[i]);
                }
            }
            rowCount = pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeAll(null, pstmt, conn);
        }
        return rowCount;
    }    // 增，删，改

    public int executeUpdateReturnId(String sql, Object[] args) {
        int id = 0;
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            // Class.forName("com.mysql.jdbc.Driver");
            // conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/warehouse",
            // "root", "123");
            conn = DBUtil.getConn();
            pstmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            if (args != null) {// 参数赋值
                for (int i = 0; i < args.length; i++) {
                    pstmt.setObject(i + 1, args[i]);
                }
            }
            pstmt.executeUpdate();
            rs = pstmt.getGeneratedKeys();
            if (rs.next()) {
                id = rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeAll(rs, pstmt, conn);
        }
        return id;
    }
}
